{**
 * general document view
 *
 * @package   OpenEMR
 * @link      http://www.open-emr.org
 * @author    Brady Miller <brady.g.miller@gmail.com>
 * @copyright Copyright (c) 2017 Brady Miller <brady.g.miller@gmail.com>
 * @license   https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
 *}

<script language="JavaScript">

 function popoutcontent(othis) {literal}{{/literal}
    let popsrc = $(othis).parents('body').find('#DocContents iframe').attr("src");
    let wname = '_' + Math.random().toString(36).substr(2, 6);
    let opt = "menubar=no,location=no,resizable=yes,scrollbars=yes,status=no";
    window.open(popsrc,wname, opt);

 return false;
 {literal}}{/literal}

 // Process click on Delete link.
 function deleteme(docid) {literal}{{/literal}
  dlgopen('interface/patient_file/deleter.php?document=' + docid, '_blank', 500, 450);
  return false;
 {literal}}{/literal}

 // Called by the deleter.php window on a successful delete.
 function imdeleted() {literal}{{/literal}
  top.restoreSession();
  window.location.href='{$REFRESH_ACTION}';
 {literal}}{/literal}

 // Called to show patient notes related to this document in the "other" frame.
 function showpnotes(docid) {literal}{{/literal}
 {literal}
 if (top.tab_mode) {
     let btnClose = '{/literal}{xl t="Done"|escape:"html"}{literal}';
     let url = top.webroot_url + '/interface/patient_file/summary/pnotes.php?docid=' + docid;
     dlgopen(url, 'pno1', 'modal-xl', 500, '', '', {
         buttons: [
             {text: btnClose, close: true, style: 'default btn-xs'}
         ],
         sizeHeight: 'auto',
         allowResize: true,
         allowDrag: true,
         dialogId: '',
         type: 'iframe'
     });
     return false;
 }
 {/literal}
  var othername = (window.name == 'RTop') ? 'RBot' : 'RTop';
  parent.left_nav.forceDual();
  parent.left_nav.loadFrame('pno1', othername, 'patient_file/summary/pnotes.php?docid=' + docid);
  return false;
 {literal}}{/literal}

 function submitNonEmpty( e ) {literal}{{/literal}
	if ( e.elements['passphrase'].value.length == 0 ) {literal}{{/literal}
		alert( "{xl t='You must enter a pass phrase to encrypt the document'}" );
	{literal}}{/literal} else {literal}{{/literal}
		e.submit();
	{literal}}{/literal}
 {literal}}{/literal}

// For tagging it encounter
function tagUpdate() {literal}{{/literal}
	var f = document.forms['document_tag'];
	if (f.encounter_check.checked) {literal}{{/literal}
		if(f.visit_category_id.value==0) {literal}{{/literal}
			alert(" {xl t='Please select visit category'}" );
			return false;
		{literal}}{/literal}
	{literal}}{/literal} else if (f.encounter_id.value == 0 ) {literal}{{/literal}
		alert(" {xl t='Please select encounter'}");
		return false;
	{literal}}{/literal}
	//top.restoreSession();
	document.forms['document_tag'].submit();
{literal}}{/literal}

// For new or existing encounter
function set_checkbox() {literal}{{/literal}
	var f = document.forms['document_tag'];
	if (f.encounter_check.checked) {literal}{{/literal}
		f.encounter_id.disabled = true;
		f.visit_category_id.disabled = false;
		$('.hide_clear').attr('href','javascript:void(0);');
	{literal}}{/literal} else {literal}{{/literal}
		f.encounter_id.disabled = false;
		f.visit_category_id.disabled = true;
		f.visit_category_id.value = 0;
		$('.hide_clear').attr('href','{$clear_encounter_tag}');
	{literal}}{/literal}
{literal}}{/literal}

// For tagging it with image procedure
function ImgProcedure() {literal}{{/literal}
	var f = document.forms['img_procedure_tag'];
	if(f.image_procedure_id.value == 0 ) {literal}{{/literal}
		alert("{xl t='Please select image procedure'}");
		return false;
	{literal}}{/literal}
	f.procedure_code.value = f.image_procedure_id.options[f.image_procedure_id.selectedIndex].getAttribute('data-code');
	document.forms['img_procedure_tag'].submit();
{literal}}{/literal}
 // Process click on Import link.
 function import_ccr(docid) {literal}{
  top.restoreSession();
  $.ajax({
    url: "library/ajax/ccr_import_ajax.php",
    type: "POST",
    dataType: "html",
    data:
    {
      ccr_ajax : "yes",
      document_id : docid,
    },
    success: function(data){
      alert(data);
      top.restoreSession();
      document.location.reload();
    },
    error:function(){
      alert("failure");
    }
  });
 }{/literal}
</script>

<table valign="top" width="100%">
    <tr>
        <td>
            <div style="margin-bottom: 6px;padding-bottom: 6px;border-bottom:3px solid gray;">
            <h4>{$file->get_url_web()|escape:'html'}
              <div class="btn-group btn-toggle">
                <button class="btn btn-xs btn-default properties">{xl t='Properties'|escape:'html'}</button>
                <button class="btn btn-xs btn-primary active">{xl t='Contents'|escape:'html'}</button>
              </div>
            <span style="float:right;">
            <a class="css_button" href='' onclick='return popoutcontent(this)' title="{xl t='Pop Out Full Screen.'|escape:'html'}">
                <span class="glyphicon glyphicon-fullscreen"></span></a>
            <a class="css_button" href="{$web_path}" title="{xl t='Original file'|escape:'html'}" onclick="top.restoreSession()"><span>{xl t='Download'|escape:'html'}</span></a>
            <a class="css_button" href='' onclick='return showpnotes({$file->get_id()})'><span>{xl t='Show Notes'|escape:'html'}</span></a>
            {$delete_string}
            {if $file->get_ccr_type($file->get_id()) eq "CCR" and ($file->get_mimetype($file->get_id()) eq "application/xml" or $file->get_mimetype($file->get_id()) eq "text/xml") and
            $file->get_imported($file->get_id()) eq 0 }
            <a class="css_button" href='javascript:' onclick='return import_ccr({$file->get_id()})'><span>{xl t='Import'|escape:'html'}</span></a>
            {/if}
            </span>
            </h4>
            </div>
        </td>
    </tr>
    <tr id="DocProperties" style="display:none;">
		<td valign="top">
			{if !$hide_encryption}
			<div class="text">
                <form method="post" name="document_encrypt" action="{$web_path}" onsubmit="return top.restoreSession()">
                <div>
                    <div style="float:left">
                        <b>{xl t='Encryption'|escape:'html'}</b>&nbsp;
                    </div>
                    <div style="float:none">
                        <a href="javascript:;" onclick="submitNonEmpty( document.forms['document_encrypt'] );">(<span>{xl t='download encrypted file'|escape:'html'})</span></a>
                    </div>
                </div>
                <div>
                    {xl t='Pass Phrase'|escape:'html'}:
                    <input title="{xl t='Supports TripleDES encryption/decryption only.'|escape:'html'} {xl t='Leaving the pass phrase blank will not encrypt the document'|escape:'html'}" type='text' size='20' name='passphrase' id='passphrase' value=''/>
                    <input type="hidden" name="encrypted" value="true"></input>
              	</div>
                </form>
            </div>
            <br/>
            {/if}
			<div class="text">
                <form method="post" name="document_validate" action="{$VALIDATE_ACTION}" onsubmit="return top.restoreSession()">
                <div>
                    <div style="float:left">
                        <b>{xl t='Sha-1 Hash'|escape:'html'}:</b>&nbsp;
                        <i>{$file->get_hash()|escape:'html'}</i>&nbsp;
                    </div>
                    <div style="float:none">
                        <a href="javascript:;" onclick="document.forms['document_validate'].submit();">(<span>{xl t='validate'|escape:'html'})</span></a>
                    </div>
                </div>
                </form>
            </div>
            <br/>
            <div class="text">
                <form method="post" name="document_update" action="{$UPDATE_ACTION}" onsubmit="return top.restoreSession()">
                <div>
                    <div style="float:left">
                        <b>{xl t='Update'|escape:'html'}</b>&nbsp;
                    </div>
                    <div style="float:none">
                        <a href="javascript:;" onclick="document.forms['document_update'].submit();">(<span>{xl t='submit'|escape:'html'})</span></a>
                    </div>
                </div>
                <div>
                    {xl t='Rename'|escape:'html'}:
                    <input type='text' size='20' name='docname' id='docname' value='{$file->get_url_web()|escape:'html'}'/>
              	</div>
                <div>
                    {xl t='Date'|escape:'html'}:
                    <input type='text' size='10' class='datepicker' name='docdate' id='docdate'
                     value='{$DOCDATE|escape:'html'}' title='{xl t='yyyy-mm-dd document date'|escape:'html'}' />
                    <select name="issue_id">{$ISSUES_LIST}</select>
                </div>
                </form>
            </div>

            <br/>

            <div class="text">
                <form method="post" name="document_move" action="{$MOVE_ACTION}" onsubmit="return top.restoreSession()">
                <div>
                    <div style="float:left">
                        <b>{xl t='Move'|escape:'html'}</b>&nbsp;
                    </div>
                    <div style="float:none">
                        <a href="javascript:;" onclick="document.forms['document_move'].submit();">(<span>{xl t='submit'|escape:'html'})</span></a>
                    </div>
                </div>

                <div>
                        <select name="new_category_id">{$tree_html_listbox}</select>&nbsp;
                        {xl t='Move to Patient'|escape:'html'} # <input type="text" name="new_patient_id" size="4" />
                        <a href="javascript:{literal}{}{/literal}"
                         onclick="top.restoreSession();var URL='controller.php?patient_finder&find&form_id={"document_move['new_patient_id']"|escape:"url"}&form_name={"document_move['new_patient_name']"|escape:"url"}'; window.open(URL, 'document_move', 'toolbar=0,scrollbars=1,location=0,statusbar=1,menubar=0,resizable=1,width=450,height=400,left=425,top=250');">
                        <img src="images/stock_search-16.png" border="0" /></a>
                        <input type="hidden" name="new_patient_name" value="" />
                </div>
                </form>
            </div>

			<br/>

			<div class="text">
			   <form method="post" name="document_tag" id="document_tag" action="{$TAG_ACTION}" onsubmit="return top.restoreSession()">

				<div >
				   <div style="float:left">
					   <b>{xl t='Tag to Encounter'|escape:'html'}</b>&nbsp;
				   </div>

				   <div style="float:none">
					   <a href="javascript:;" onclick="tagUpdate();">(<span>{xl t='submit'|escape:'html'})</span></a>
				   </div>
			   </div>

				 <div>
					<select id="encounter_id"  name="encounter_id"  >{$ENC_LIST}</select>&nbsp;
					<a href="{$clear_encounter_tag}" class="hide_clear">(<span>{xl t='clear'|escape:'html'})</span></a>&nbsp;&nbsp;
					<input type="checkbox" name="encounter_check" id="encounter_check"  onclick='set_checkbox(this)'/> <label for="encounter_check"><b>{xl t='Create Encounter'|escape:'html'}</b></label>&nbsp;&nbsp;
					   {xl t='Visit Category'|escape:'html'} : &nbsp;<select id="visit_category_id"  name="visit_category_id"  disabled>{$VISIT_CATEGORY_LIST}</select>&nbsp;

			   </div>
			   </form>
		   </div>
		   <br/>
		   <div class="text">
			<form method="post" name="img_procedure_tag" id="img_procedure_tag" action="{$IMG_PROCEDURE_TAG_ACTION}" onsubmit="return top.restoreSession()">
			<input type='hidden' name='procedure_code' value=''>
			<div>
				<div style="float:left">
					<b>{xl t='Tag to Image Procedure'|escape:'html'}</b>&nbsp;
				</div>
				<div style="float:none">
					<a href="javascript:;" onclick="ImgProcedure();">(<span>{xl t='submit'|escape:'html'})</span></a>
				</div>
			</div>
			<div>
				<select id="image_procedure_id"  name="image_procedure_id">{$IMAGE_PROCEDURE_LIST}</select>&nbsp;
				<a href="{$clear_procedure_tag}">(<span>{xl t='clear'|escape:'html'})</span></a>
			</div>
			</form>
		   </div>

            <br/>

            <form name="notes" method="post" action="{$NOTE_ACTION}" onsubmit="return top.restoreSession()">
            <div class="text">
                <div>
                    <div style="float:left">
                        <b>{xl t='Notes'|escape:'html'}</b>&nbsp;
                    </div>
                    <div style="float:none">
                        <a href="javascript:;" onclick="document.notes.identifier.value='no';document.forms['notes'].submit();">(<span>{xl t='add'|escape:'html'}</span>)</a>
                    	&nbsp;&nbsp;&nbsp;<b>{xl t='Email'|escape:'html'}</b>&nbsp;
                    	<input type="text" size="25" name="provide_email" id="provide_email" />
                    	<input type="hidden" name="identifier" id="identifier" />
                        <a href="javascript:;" onclick="javascript:document.notes.identifier.value='yes';document.forms['notes'].submit();">
                        	(<span>{xl t='Send'|escape:'html'}</span>)
                        </a>
                    </div>
                    <div>

                    </div>
                    <div style="float:none">

                    </div>
                <div>
                    <textarea cols="53" rows="8" wrap="virtual" name="note" style="width:100%"></textarea><br>
                    <input type="hidden" name="process" value="{$PROCESS|escape:'html'}" />
                    <input type="hidden" name="foreign_id" value="{$file->get_id()|escape:'html'}" />

                    {if $notes}
                    <div style="margin-top:7px">
                        {foreach name=note_loop from=$notes item=note}
                        <div>
                        {xl t='Note'|escape:'html'} #{$note->get_id()|escape:'html'}
                        {xl t='Date:'|escape:'html'} {$note->get_date()|escape:'html'}
                        {$note->get_note()|escape:'html'}
                        {if $note->get_owner()}
                            &nbsp;-{user_info id=$note->get_owner()}
                        {/if}
                        </div>
                        {/foreach}
                    {/if}
                    </div>
                </div>
            </div>
            </form>
            <h4>{xl t='Contents'|escape:'html'}</h4>
		</td>
	</tr>
	<tr id="DocContents">
		<td>
            {if $file->get_mimetype() eq "image/tiff"}
			<embed frameborder="0" type="{$file->get_mimetype()}" src="{$web_path}as_file=false"></embed>
			{elseif $file->get_mimetype() eq "image/png" or
			 $file->get_mimetype() eq "image/jpg" or
			 $file->get_mimetype() eq "image/jpeg" or
			 $file->get_mimetype() eq "image/gif" or
			 $file->get_mimetype() eq "application/pdf" }
			<iframe frameborder="0" type="{$file->get_mimetype()}" src="{$web_path}as_file=false"></iframe>
            {elseif $file->get_mimetype() eq "application/dicom"}
            <!-- DWV -->
            <div id="dwv" src="{$web_path}as_file=false">
                <!-- Toolbar -->
                <div class="toolbar"></div>
                <div class="warn_diagnostic">{xl t='Not For Diagnostic Use'|escape:'html'}</div>
                <!-- Layer Container -->
                <div class="layerContainer">
                    <canvas class="imageLayer">Only for HTML5 compatible browsers...</canvas>
                </div><!-- /layerContainer -->
            </div><!-- /dwv -->
            <!-- Third party (dwv) -->
            <script type="text/javascript" src="{$assets_static_relative}/modernizr-3-5-0/dist/modernizr-build.js"></script>
            <script type="text/javascript" src="{$assets_static_relative}/i18next-9-0-1/i18next.min.js"></script>
            <script type="text/javascript" src="{$assets_static_relative}/i18next-xhr-backend-1-4-3/i18nextXHRBackend.min.js"></script>
            <script type="text/javascript" src="{$assets_static_relative}/i18next-browser-languagedetector-2-0-0/i18nextBrowserLanguageDetector.min.js"></script>
            <script type="text/javascript" src="{$assets_static_relative}/konva-1-6-8/konva.min.js"></script>
            <script type="text/javascript" src="{$assets_static_relative}/magic-wand-js/js/magic-wand-min.js"></script>
            <script type="text/javascript" src="{$assets_static_relative}/jszip-3-1-5/dist/jszip.min.js"></script>
            <!-- Third party (viewer) -->
            <script type="text/javascript" src="{$assets_static_relative}/flot-0-8-3/jquery.flot.js"></script>
            <!-- decoders -->
            <script type="text/javascript" src="{$assets_static_relative}/dwv-0-21-0/decoders/pdfjs/jpx.js"></script>
            <script type="text/javascript" src="{$assets_static_relative}/dwv-0-21-0/decoders/pdfjs/util.js"></script>
            <script type="text/javascript" src="{$assets_static_relative}/dwv-0-21-0/decoders/pdfjs/arithmetic_decoder.js"></script>
            <script type="text/javascript" src="{$assets_static_relative}/dwv-0-21-0/decoders/pdfjs/jpg.js"></script>
            <script type="text/javascript" src="{$assets_static_relative}/dwv-0-21-0/decoders/rii-mango/lossless-min.js"></script>
            <!-- Local (dwv) -->
            <script type="text/javascript" src="{$assets_static_relative}/dwv-0-21-0/dist/dwv.min.js"></script>
            <!-- i18n dwv wrapper -->
            <script type="text/javascript" src="{$webroot}/library/js/dwv/dwv_i18n.js"></script>
            <!-- Main -->
            <script type="text/javascript" src="{$webroot}/library/js/dwv/dicom_gui.js"></script>
            <script type="text/javascript" src="{$webroot}/library/js/dwv/dicom_launcher.js"></script>
            {elseif $file->get_ccr_type($file->get_id()) ne "CCR" and $file->get_ccr_type($file->get_id()) ne "CCD"}
            <iframe frameborder="0" type="{$file->get_mimetype()}" src="{$web_path}as_file=true"></iframe>
			{/if}
		</td>
	</tr>
</table>
<script language='JavaScript'>
{literal}
$('.btn-toggle').click(function() {
    $(this).find('.btn').toggleClass('active');

    if ($(this).find('.btn-primary').length >0) {
        $(this).find('.btn').toggleClass('btn-primary');
    }

    $(this).find('.btn').toggleClass('btn-default');
    var show_prop = ($(this).find('.properties.active').length > 0 ? 'block':'none');
    $("#DocProperties").css('display', show_prop);
});
{/literal}
</script>
